package service.impl;

import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import param.lock.Lockrequest;
import param.lock.Lockresponse;
import param.orderquery.Orderqueryrequest;
import param.orderquery.Orderqueryresponse;
import param.pub.Publicrequest;
import param.pub.Publicresponse;
import param.residueseatquery.Residueseatqueryrequest;
import param.residueseatquery.Residueseatqueryresponse;
import param.returnticket.Returnrequest;
import param.returnticket.Returnresponse;
import param.sell.Sellrequest;
import param.sell.Sellresponse;
import param.taketicket.Taketicketrequest;
import param.taketicket.Taketicketresponse;
import param.testcon.Testconresponse;
import param.unlock.Unlockrequest;
import param.unlock.Unlockresponse;
import service.InvokeService;
import utils.PublicInfoUtil;
import utils.PublicXmlUtil;
import utils.XmlUtil;
import cn.nova.hljbus.ws.NetTicketSellServiceImplSoapProxy;

public class HLJFInvokeServiceImpl implements InvokeService{

	final private Logger log = LoggerFactory.getLogger(getClass());
	private String webservice = "/WebServiceDBServer/NetSaleServiceImplService.asmx";
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");

	@Override
	public Lockresponse lock(String serversaddress, Lockrequest obj) {
		String xmlString = null;
		String resultXml = null;
		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
		obj.setPublicrequest(publicrequest);
		Lockresponse response = null;
		try {
			//判断座位号是否为空
			for (int i = 0; i < obj.getTickets().size(); i++) {
				if(obj.getTickets().get(i).getSeatno()==null || ("").equals(obj.getTickets().get(i).getSeatno())){
					int j=i+1;
					obj.getTickets().get(i).setSeatno("0"+j);
				}
			}
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
			xmlString = PublicInfoUtil.toXml(obj);
			resultXml = proxy.lock(xmlString);
			response = (Lockresponse) XmlUtil.toBean(resultXml, Lockresponse.class);
			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
			for (int i = 0; i < response.getTickets().size(); i++) {
				int seatno=Integer.parseInt(response.getTickets().get(i).getSeatno());
				if(seatno< 10){
					response.getTickets().get(i).setSeatno(String.valueOf(seatno));
				}
			}
		} catch (Exception e) {
			log.error("黑龙江锁位调用出错", e);
			log.error("锁位请求字符串：" + xmlString);
			log.error("锁位返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Residueseatqueryresponse residueseatquery(String serversaddress, Residueseatqueryrequest obj) {
		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
		obj.setPublicrequest(publicrequest);
		Residueseatqueryresponse response = null;
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
			resultXml = proxy.residueseatquery(xmlString);
			response = (Residueseatqueryresponse) XmlUtil.toBean(resultXml, Residueseatqueryresponse.class);
			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			log.error("黑龙江余票查询调用出错", e);
			log.error("余票查询请求字符串：" + xmlString);
			log.error("余票查询返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Unlockresponse unlock(String serversaddress, Unlockrequest obj) {
		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
		obj.setPublicrequest(publicrequest);
		Unlockresponse response = null;
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
			resultXml = proxy.unlock(xmlString);
			response = (Unlockresponse) XmlUtil.toBean(resultXml, Unlockresponse.class);
			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			log.error("黑龙江解锁调用出错", e);
			log.error("解锁请求字符串：" + xmlString);
			log.error("解锁返回字符串：" + resultXml);
		}
		return response;		
	}

	@Override
	public Sellresponse sell(String serversaddress, Sellrequest obj) {
		String xmlString = null;
		String resultXml = null;
		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
		obj.setPublicrequest(publicrequest);
		Sellresponse response = null;
		try {
			for (int i = 0; i < obj.getTickets().size(); i++) {
				int seatno=Integer.parseInt(obj.getTickets().get(i).getSeatno());
				if(seatno< 10){
					obj.getTickets().get(i).setSeatno("0"+seatno);
				}
			}
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
			resultXml = proxy.sell(xmlString);
			response = (Sellresponse) XmlUtil.toBean(resultXml, Sellresponse.class);
			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
			response.setPublicresponse(publicresponse);

			for (int i = 0; i < response.getTickets().size(); i++) {
				int seatno=Integer.parseInt(response.getTickets().get(i).getSeatno());
				if(seatno< 10){
					response.getTickets().get(i).setSeatno(String.valueOf(seatno));
				}
			}
		} catch (Exception e) {
			log.error("黑龙江售票调用出错", e);
			log.error("售票请求字符串：" + xmlString);
			log.error("售票返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Orderqueryresponse orderquery(String serversaddress, Orderqueryrequest obj) {
		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
		obj.setPublicrequest(publicrequest);
		Orderqueryresponse response = null;
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
			resultXml = proxy.orderquery(xmlString);
			response = (Orderqueryresponse) XmlUtil.toBean(resultXml, Orderqueryresponse.class);
			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
			for (int i = 0; i < response.getTickets().size(); i++) {
				int seatno=Integer.parseInt(response.getTickets().get(i).getSeatno());
				if(seatno< 10){
					response.getTickets().get(i).setSeatno(String.valueOf(seatno));
				}
			}
		} catch (Exception e) {
			log.error("黑龙江订单查询调用出错", e);
			log.error("订单查询请求字符串：" + xmlString);
			log.error("订单查询返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Taketicketresponse taketicket(String serversaddress, Taketicketrequest obj) {
		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
		obj.setPublicrequest(publicrequest);
		Taketicketresponse response = null;
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
			resultXml = proxy.taketicket(xmlString);
			response = (Taketicketresponse) XmlUtil.toBean(resultXml, Taketicketresponse.class);
			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			log.error("黑龙江取票调用出错", e);
			log.error("取票请求字符串：" + xmlString);
			log.error("取票返回字符串：" + resultXml);
		}
		return response;
	}

//	@Override
//	public Changeticketnoresponse changeticketno(String serversaddress, Changeticketnorequest obj) {
//		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
//		obj.setPublicrequest(publicrequest);
//		Changeticketnoresponse response = null;
//		String xmlString = null;
//		String resultXml = null;
//		try {
//			xmlString = PublicInfoUtil.toXml(obj);
//			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
//			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
//			resultXml = proxy.changeticketno(xmlString);
//			response = (Changeticketnoresponse) XmlUtil.toBean(resultXml, Changeticketnoresponse.class);
//			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
//			response.setPublicresponse(publicresponse);
//		} catch (Exception e) {
//			log.error("黑龙江换票号调用出错", e);
//			log.error("换票号请求字符串：" + xmlString);
//			log.error("换票号返回字符串：" + resultXml);
//		}
//		return response;
//	}

	@Override
	public Testconresponse saletestcon(String serversaddress) {
		Testconresponse response = new Testconresponse();
		Publicresponse publicresponse = PublicXmlUtil.newPublicresponse();
		response.setPublicresponse(publicresponse);
		try {
			URL url = new URL("http://" + serversaddress + webservice + "?wsdl"); 
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");// 提交模式
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setConnectTimeout(6000);

			new DataOutputStream(conn.getOutputStream());
		} catch (Exception e) {
			response.getPublicresponse().setResultcode("NATEST1002");
			response.getPublicresponse().setResultmsg("业务连通性接口调用失败");
			log.error(serversaddress + "黑龙江业务连通性接口调用失败", e);
		}
		return response;
	}

	@Override
	public Returnresponse returnticket(String serversaddress, Returnrequest obj) {
		String xmlString = null;
		String resultXml = null;
		Publicrequest publicrequest = DateFormatRequest(obj.getPublicrequest());
		obj.setPublicrequest(publicrequest);
		Returnresponse response = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplSoapProxy proxy = new NetTicketSellServiceImplSoapProxy(netTicketSellServiceImplPort_address);
			resultXml = proxy.returnticket(xmlString);
			response = (Returnresponse) XmlUtil.toBean(resultXml, Returnresponse.class);
			Publicresponse publicresponse = DateFormatResponse(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			log.error("黑龙江退票调用出错", e);
			log.error("退票请求字符串：" + xmlString);
			log.error("退票返回字符串：" + resultXml);
		}
		return response;
	}


	private Publicrequest DateFormatRequest(Publicrequest request) {
		String asktime = request.getAsktime();
		try {
			request.setAsktime(sdf1.format((sdf.parse(asktime))));
		} catch (ParseException e) {
			log.error("请求时间日期类型转换失败", e);
		}
		return request;
	}
	
	private Publicresponse DateFormatResponse(Publicresponse response) {
		String returntime = response.getReturntime();
		try {
			response.setReturntime(sdf.format((sdf1.parse(returntime))));
		} catch (ParseException e) {
			log.error("退票时间日期类型转换失败", e);
		}
		return response;
	}

	@Override
	public Testconresponse synctestcon(String serversaddress) {
		Testconresponse response = new Testconresponse();
		Publicresponse publicresponse = PublicXmlUtil.newPublicresponse();
		response.setPublicresponse(publicresponse);
		return response;
	}

	@Override
	public Returnresponse specialreturnticket(String serversaddress,
			Returnrequest obj) throws Exception {
		
		return null;
	}

}
